﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IObjects;
using Entities;
using DataObjects.EFData;
using Utilities;

namespace DataObjects
{
    public class MyBranches : IMyBranches
    {
        private MyBranchesInfo _myBranchesInfo;
        private string mode = string.Empty;
        private WebOnlineEntities datacontext;
        #region implement interface
        public MyBranchesInfo MyCompanyInfo
        {
            get
            {
                return _myBranchesInfo;
            }
            set
            {
                _myBranchesInfo = value;
            }
        }

        public string Mode
        {
            get
            {
                return mode;
            }
            set
            {
                mode = value;
            }
        }

        public int Save()
        {
            if (mode == Constants.MODE_INSERT)
            {
                return Insert();
            }
            if (mode == Constants.MODE_UPDATE)
            {
                return Update();
            }
            if (mode == Constants.MODE_INSERT)
            {
                return Delete();
            }
            throw new Exception("Mode is invalid in MyBranches object");
        }

        public IList<MyBranchesInfo> GetMyBranches()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var myBrachesList = datacontext.TB_MyBranches.Select(c => c.ToMyBranchesInfo()).ToList();
                return myBrachesList;
            }
        }

        public IList<MyBranchesInfo> GetMyBranhesByCompanyId(Guid myCompanyId)
        {
            using (datacontext = new WebOnlineEntities())
            {
                var myBrachesList = datacontext.TB_MyBranches.Where(c => c.CompanyId == myCompanyId).Select(c => c.ToMyBranchesInfo()).ToList();
                return myBrachesList;
            }
        }

        public MyBranchesInfo GetById(int id)
        {
            using (datacontext = new WebOnlineEntities())
            {
                var myBranches = datacontext.TB_MyBranches.Where(c => c.ID == id).FirstOrDefault();
                if (myBranches != null)
                {
                    return myBranches.ToMyBranchesInfo();
                }
                return null;
            }
        }
        #endregion
        #region private methods
        private int Delete()
        {
            using (datacontext = new WebOnlineEntities())
            {
                datacontext.DeleteObject(datacontext.TB_MyBranches.FirstOrDefault(c => c.ID == _myBranchesInfo.Id));
                return datacontext.SaveChanges();
            }
        }

        private int Update()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var myBranches = datacontext.TB_MyBranches.Where(c => c.ID == _myBranchesInfo.Id).FirstOrDefault();
                myBranches.IsHeadQuarter = _myBranchesInfo.IsHeadQuarter;
                myBranches.Phone = _myBranchesInfo.Phone;
                myBranches.Address = _myBranchesInfo.Address;
                myBranches.BranchName = _myBranchesInfo.BranchName;
                myBranches.Description = _myBranchesInfo.Description;
                myBranches.Email = _myBranchesInfo.Email;
                myBranches.Fax = _myBranchesInfo.Fax;
                myBranches.CompanyId = _myBranchesInfo.CompanyId;
      
                return datacontext.SaveChanges();
            }
        }

        private int Insert()
        {
            using (datacontext = new WebOnlineEntities())
            {
                var branch = _myBranchesInfo.ToTB_MyBranches();
                datacontext.AddToTB_MyBranches(branch);
                return datacontext.SaveChanges();
            }
        }
        #endregion
    }
}
